package k8s

import (
	"k8s.io/api/core/v1"
	batchv1 "k8s.io/api/batch/v1"
	"k8s.io/client-go/kubernetes/scheme"
	appsv1 "k8s.io/api/apps/v1"

)

type Event struct {
	Name string				`json:"name"`
	Kind string				`json:"kind"`
	ChildObject string		`json:"childobject"`
	Num int32				`json:"num"`
	Type string				`json:"type"`
	Source string			`json:"source"`
	Message string			`json:"message"`
	Reason string			`json:"reason"`
	LastOccurTime string	`json:"lastoccurtime"`
}

func getEventsForNode(node *v1.Node)(evns []Event)  {
	//node, err := clientset.CoreV1().Events().Search(name,metav1.GetOptions{})
	events, _ := clientset.CoreV1().Events("").Search(scheme.Scheme, node)
	for _,item := range events.Items {

		evns = append(evns,Event{
			Message: item.Message,
			Source: item.Source.Component + " " + item.Source.Host,
			ChildObject: item.InvolvedObject.FieldPath,
			Reason: item.Reason,
			Num: item.Count,
			LastOccurTime: item.LastTimestamp.Format("2006-01-02 15:04:05"),
		})
	}
	return
}

func getEventsForJob(job *batchv1.Job)(evns []Event)  {
	//node, err := clientset.CoreV1().Events().Search(name,metav1.GetOptions{})
	events, _ := clientset.CoreV1().Events("").Search(scheme.Scheme, job)
	for _,item := range events.Items {

		evns = append(evns,Event{
			Message: item.Message,
			Source: item.Source.Component + " " + item.Source.Host,
			ChildObject: item.InvolvedObject.FieldPath,
			Reason: item.Reason,
			Num: item.Count,
			LastOccurTime: item.LastTimestamp.Format("2006-01-02 15:04:05"),
		})
	}
	return
}

func getEventsForSts(sts *appsv1.StatefulSet)(evns []Event)  {
	//node, err := clientset.CoreV1().Events().Search(name,metav1.GetOptions{})
	events, _ := clientset.CoreV1().Events("").Search(scheme.Scheme, sts)
	for _,item := range events.Items {

		evns = append(evns,Event{
			Message: item.Message,
			Source: item.Source.Component + " " + item.Source.Host,
			ChildObject: item.InvolvedObject.FieldPath,
			Reason: item.Reason,
			Num: item.Count,
			LastOccurTime: item.LastTimestamp.Format("2006-01-02 15:04:05"),
		})
	}
	return
}


